package com.hq.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bean.Goods;
import com.hq.service.GoodsService;
import com.hq.util.FileUploadUtil;
import com.hq.util.SnowflakeIdUtils;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GoodsController {
    @Resource
    GoodsService goodsService;

    @RequestMapping("/list.do")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum, Model model){
        Page<Object> page = PageHelper.startPage(pageNum, 5);
        List list=goodsService.list();
        model.addAttribute("list",list);
        model.addAttribute("page",page);
        return "list";
    }

    @RequestMapping("/addy.do")
    public String addy(HttpSession session){
        String s = new SnowflakeIdUtils(1, 1).nextId() + "";
        session.setAttribute("card",s);
        return "add";
    }
    @RequestMapping("/updy.do")
    public String addy(Integer id){
        return "upd";
    }
    @RequestMapping("/findallbid.do")
    @ResponseBody
    public Object findallbid(){
        return goodsService.findallbid();
    }

    @RequestMapping("/addGoods.do")
    @ResponseBody
    public int addGoods(Goods goods, MultipartFile photo,HttpSession session){
        if(photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            goods.setPic(upload);
        }
        return goodsService.addGoods(goods);
    }
    @RequestMapping("/updGoods.do")
    @ResponseBody
    public int updGoods(Goods goods, MultipartFile photo,HttpSession session){
        if(photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            goods.setPic(upload);
        }
        return goodsService.updGoods(goods);
    }
    @RequestMapping("/delids.do")
    @ResponseBody
    public int delids(String id){
        return goodsService.delids(id);
    }
    @RequestMapping("/findgoods.do")
    @ResponseBody
    public Object findgoods(Integer id){
        return goodsService.findgoods(id);
    }
}
